#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
交易 - B2C微支付相关模型
"""

from typing import Dict, Any, Optional, List
from jlpay.model.base import OrgBaseReq, BaseResponse
from jlpay.model.trans.dto import CouponInfoDto, GoodsDataDto, TerminalInfoDto


class MicroPayRequest(OrgBaseReq):
    """微支付请求"""
    
    API_URL = "/open/trans/micropay"
    
    def __init__(self):
        """初始化微支付请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.pay_type = None  # 交易类型
        self.out_trade_no = None  # 外部订单号
        self.payment_valid_time = None  # 订单有效期
        self.body = None  # 商品标题
        self.attach = None  # 商品描述
        self.total_fee = None  # 交易金额
        self.auth_code = None  # 用户付款码
        self.limit_pay = None  # 指定支付方式
        self.is_hire_purchase = None  # 分期标识
        self.hire_purchase_num = None  # 分期标识
        self.hire_purchase_seller_percent = None  # 卖家承担的手续费比例
        self.remark = None  # 备注
        self.op_user_id = None  # 操作员
        self.op_shop_id = None  # 门店号
        self.device_info = None  # 终端设备号
        self.mch_create_ip = None  # 商户IP
        self.longitude = None  # 经度
        self.latitude = None  # 纬度
        self.sub_appid = None  # 子商户appid
        self.goods_tag = None  # 商品标记
        self.goods_data = None  # 商品详情
        self.terminal_info = None  # 终端信息
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
    
    def set_term_no(self, term_no: str):
        """设置终端号"""
        self.term_no = term_no
    
    def set_pay_type(self, pay_type: str):
        """设置交易类型"""
        self.pay_type = pay_type
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
    
    def set_payment_valid_time(self, payment_valid_time: str):
        """设置订单有效期"""
        self.payment_valid_time = payment_valid_time
    
    def set_body(self, body: str):
        """设置商品标题"""
        self.body = body
    
    def set_attach(self, attach: str):
        """设置商品描述"""
        self.attach = attach
    
    def set_total_fee(self, total_fee: str):
        """设置交易金额"""
        self.total_fee = total_fee
    
    def set_auth_code(self, auth_code: str):
        """设置用户付款码"""
        self.auth_code = auth_code
    
    def set_limit_pay(self, limit_pay: str):
        """设置指定支付方式"""
        self.limit_pay = limit_pay
    
    def set_is_hire_purchase(self, is_hire_purchase: str):
        """设置分期标识"""
        self.is_hire_purchase = is_hire_purchase
    
    def set_hire_purchase_num(self, hire_purchase_num: str):
        """设置分期标识"""
        self.hire_purchase_num = hire_purchase_num
    
    def set_hire_purchase_seller_percent(self, hire_purchase_seller_percent: str):
        """设置卖家承担的手续费比例"""
        self.hire_purchase_seller_percent = hire_purchase_seller_percent
    
    def set_remark(self, remark: str):
        """设置备注"""
        self.remark = remark
    
    def set_op_user_id(self, op_user_id: str):
        """设置操作员"""
        self.op_user_id = op_user_id
    
    def set_op_shop_id(self, op_shop_id: str):
        """设置门店号"""
        self.op_shop_id = op_shop_id
    
    def set_device_info(self, device_info: str):
        """设置终端设备号"""
        self.device_info = device_info
    
    def set_mch_create_ip(self, mch_create_ip: str):
        """设置商户IP"""
        self.mch_create_ip = mch_create_ip
    
    def set_longitude(self, longitude: str):
        """设置经度"""
        self.longitude = longitude
    
    def set_latitude(self, latitude: str):
        """设置纬度"""
        self.latitude = latitude
    
    def set_sub_appid(self, sub_appid: str):
        """设置子商户appid"""
        self.sub_appid = sub_appid
    
    def set_goods_tag(self, goods_tag: str):
        """设置商品标记"""
        self.goods_tag = goods_tag
    
    def set_goods_data(self, goods_data: GoodsDataDto):
        """设置商品详情"""
        self.goods_data = goods_data
    
    def set_terminal_info(self, terminal_info: TerminalInfoDto):
        """设置终端信息"""
        self.terminal_info = terminal_info
    
    def to_dict(self) -> Dict[str, Any]:
        """
        转换为字典格式
        
        Returns:
            字典格式的数据
        """
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'pay_type': self.pay_type,
            'out_trade_no': self.out_trade_no,
            'payment_valid_time': self.payment_valid_time,
            'body': self.body,
            'attach': self.attach,
            'total_fee': self.total_fee,
            'auth_code': self.auth_code,
            'limit_pay': self.limit_pay,
            'is_hire_purchase': self.is_hire_purchase,
            'hire_purchase_num': self.hire_purchase_num,
            'hire_purchase_seller_percent': self.hire_purchase_seller_percent,
            'remark': self.remark,
            'op_user_id': self.op_user_id,
            'op_shop_id': self.op_shop_id,
            'device_info': self.device_info,
            'mch_create_ip': self.mch_create_ip,
            'longitude': self.longitude,
            'latitude': self.latitude,
            'sub_appid': self.sub_appid,
            'goods_tag': self.goods_tag
        })
        
        if self.goods_data:
            data['goods_data'] = self.goods_data.to_dict()
        
        if self.terminal_info:
            data['terminal_info'] = self.terminal_info.to_dict()
        
        return data


class MicroPayResponse(BaseResponse):
    """微支付响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化微支付响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 1：待确认2：成功3：失败
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.device_info = None  # 终端设备号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.chn_transaction_id = None  # 渠道订单号
        self.total_fee = None  # 交易金额
        self.order_time = None  # 订单时间
        self.trans_time = None  # 交易时间
        self.finnal_amount = None  # 实际付款金额
        self.discount_amount = None  # 优惠总金额
        self.discount_name = None  # 优惠活动名称
        self.coupon_info = None  # 优惠详情
        self.pay_type = None  # 交易类型
        self.sub_openid = None  # 用户openid
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.term_no = response_dict.get('term_no')
            self.device_info = response_dict.get('device_info')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.chn_transaction_id = response_dict.get('chn_transaction_id')
            self.total_fee = response_dict.get('total_fee')
            self.order_time = response_dict.get('order_time')
            self.trans_time = response_dict.get('trans_time')
            self.finnal_amount = response_dict.get('finnal_amount')
            self.discount_amount = response_dict.get('discount_amount')
            self.discount_name = response_dict.get('discount_name')
            self.coupon_info = response_dict.get('coupon_info')
            self.pay_type = response_dict.get('pay_type')
            self.sub_openid = response_dict.get('sub_openid')
    
    def to_dict(self) -> Dict[str, Any]:
        """
        转换为字典格式
        
        Returns:
            字典格式的数据
        """
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'device_info': self.device_info,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'chn_transaction_id': self.chn_transaction_id,
            'total_fee': self.total_fee,
            'order_time': self.order_time,
            'trans_time': self.trans_time,
            'finnal_amount': self.finnal_amount,
            'discount_amount': self.discount_amount,
            'discount_name': self.discount_name,
            'coupon_info': self.coupon_info,
            'pay_type': self.pay_type,
            'sub_openid': self.sub_openid
        })
        return data


class MicroPayPreAuthRequest(OrgBaseReq):
    """微支付预授权请求"""
    
    API_URL = "/open/trans/micropaypreauth"
    
    def __init__(self):
        """初始化微支付预授权请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.pay_type = None  # 交易类型
        self.out_trade_no = None  # 外部订单号
        self.payment_valid_time = None  # 订单有效期
        self.guarantee_expiry = None  # 预授权交易有效期
        self.body = None  # 商品标题
        self.attach = None  # 商品描述
        self.total_fee = None  # 交易金额
        self.auth_code = None  # 用户付款码
        self.remark = None  # 备注
        self.op_user_id = None  # 操作员
        self.op_shop_id = None  # 门店号
        self.device_info = None  # 终端设备号
        self.mch_create_ip = None  # 终端IP
        self.trans_address = None  # 交易地址
        self.area_code = None  # 交易地区码
        self.longitude = None  # 经度
        self.latitude = None  # 纬度
        self.sync_able = None  # 是否同步
        self.sub_appid = None  # 公众账号ID
        self.terminal_info = None  # 终端信息
    
    def set_mch_id(self, mch_id: str):
        """
        设置商户号
        
        Args:
            mch_id: 商户号
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.mch_id = mch_id
        return self
    
    def set_term_no(self, term_no: str):
        """
        设置终端号
        
        Args:
            term_no: 终端号
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.term_no = term_no
        return self
    
    def set_pay_type(self, pay_type: str):
        """
        设置交易类型
        
        Args:
            pay_type: 交易类型
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.pay_type = pay_type
        return self
    
    def set_out_trade_no(self, out_trade_no: str):
        """
        设置外部订单号
        
        Args:
            out_trade_no: 外部订单号
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.out_trade_no = out_trade_no
        return self
    
    def set_payment_valid_time(self, payment_valid_time: str):
        """
        设置订单有效期
        
        Args:
            payment_valid_time: 订单有效期
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.payment_valid_time = payment_valid_time
        return self
    
    def set_guarantee_expiry(self, guarantee_expiry: str):
        """
        设置预授权交易有效期
        
        Args:
            guarantee_expiry: 预授权交易有效期
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.guarantee_expiry = guarantee_expiry
        return self
    
    def set_body(self, body: str):
        """
        设置商品标题
        
        Args:
            body: 商品标题
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.body = body
        return self
    
    def set_attach(self, attach: str):
        """
        设置商品描述
        
        Args:
            attach: 商品描述
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.attach = attach
        return self
    
    def set_total_fee(self, total_fee: str):
        """
        设置交易金额
        
        Args:
            total_fee: 交易金额
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.total_fee = total_fee
        return self
    
    def set_auth_code(self, auth_code: str):
        """
        设置用户付款码
        
        Args:
            auth_code: 用户付款码
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.auth_code = auth_code
        return self
    
    def set_remark(self, remark: str):
        """
        设置备注
        
        Args:
            remark: 备注
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.remark = remark
        return self
    
    def set_op_user_id(self, op_user_id: str):
        """
        设置操作员
        
        Args:
            op_user_id: 操作员
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.op_user_id = op_user_id
        return self
    
    def set_op_shop_id(self, op_shop_id: str):
        """
        设置门店号
        
        Args:
            op_shop_id: 门店号
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.op_shop_id = op_shop_id
        return self
    
    def set_device_info(self, device_info: str):
        """
        设置终端设备号
        
        Args:
            device_info: 终端设备号
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.device_info = device_info
        return self
    
    def set_mch_create_ip(self, mch_create_ip: str):
        """
        设置终端IP
        
        Args:
            mch_create_ip: 终端IP
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.mch_create_ip = mch_create_ip
        return self
    
    def set_trans_address(self, trans_address: str):
        """
        设置交易地址
        
        Args:
            trans_address: 交易地址
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.trans_address = trans_address
        return self
    
    def set_area_code(self, area_code: str):
        """
        设置交易地区码
        
        Args:
            area_code: 交易地区码
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.area_code = area_code
        return self
    
    def set_longitude(self, longitude: str):
        """
        设置经度
        
        Args:
            longitude: 经度
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.longitude = longitude
        return self
    
    def set_latitude(self, latitude: str):
        """
        设置纬度
        
        Args:
            latitude: 纬度
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.latitude = latitude
        return self
    
    def set_sync_able(self, sync_able: bool):
        """
        设置是否同步
        
        Args:
            sync_able: 是否同步
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.sync_able = sync_able
        return self
    
    def set_sub_appid(self, sub_appid: str):
        """
        设置公众账号ID
        
        Args:
            sub_appid: 公众账号ID
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.sub_appid = sub_appid
        return self
    
    def set_terminal_info(self, terminal_info: TerminalInfoDto):
        """
        设置终端信息
        
        Args:
            terminal_info: 终端信息
            
        Returns:
            MicroPayPreAuthRequest实例
        """
        self.terminal_info = terminal_info
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """
        转换为字典格式
        
        Returns:
            字典格式的数据
        """
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'pay_type': self.pay_type,
            'out_trade_no': self.out_trade_no,
            'payment_valid_time': self.payment_valid_time,
            'guarantee_expiry': self.guarantee_expiry,
            'body': self.body,
            'attach': self.attach,
            'total_fee': self.total_fee,
            'auth_code': self.auth_code,
            'remark': self.remark,
            'op_user_id': self.op_user_id,
            'op_shop_id': self.op_shop_id,
            'device_info': self.device_info,
            'mch_create_ip': self.mch_create_ip,
            'trans_address': self.trans_address,
            'area_code': self.area_code,
            'longitude': self.longitude,
            'latitude': self.latitude,
            'sync_able': self.sync_able,
            'sub_appid': self.sub_appid,
            'terminal_info': self.terminal_info.to_dict() if self.terminal_info else None
        })
        return data


class MicroPayPreAuthResponse(BaseResponse):
    """微支付预授权响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化微支付预授权响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 1：待确认 2：成功 3：失败
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.device_info = None  # 终端设备号
        self.guarantee_auth_code = None  # 授权码
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.chn_transaction_id = None  # 渠道订单号
        self.total_fee = None  # 交易金额
        self.order_time = None  # 订单时间
        self.trans_time = None  # 交易时间
        self.finnal_amount = None  # 实际付款金额
        self.discount_amount = None  # 优惠总金额
        self.discount_name = None  # 优惠活动名称
        self.coupon_info = None  # 优惠信息
        self.pay_type = None  # 交易类型
        self.sub_openid = None  # 用户openid
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.term_no = response_dict.get('term_no')
            self.device_info = response_dict.get('device_info')
            self.guarantee_auth_code = response_dict.get('guarantee_auth_code')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.chn_transaction_id = response_dict.get('chn_transaction_id')
            self.total_fee = response_dict.get('total_fee')
            self.order_time = response_dict.get('order_time')
            self.trans_time = response_dict.get('trans_time')
            self.finnal_amount = response_dict.get('finnal_amount')
            self.discount_amount = response_dict.get('discount_amount')
            self.discount_name = response_dict.get('discount_name')
            self.coupon_info = response_dict.get('coupon_info')
            self.pay_type = response_dict.get('pay_type')
            self.sub_openid = response_dict.get('sub_openid')
    
    def to_dict(self) -> Dict[str, Any]:
        """
        转换为字典格式
        
        Returns:
            字典格式的数据
        """
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'device_info': self.device_info,
            'guarantee_auth_code': self.guarantee_auth_code,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'chn_transaction_id': self.chn_transaction_id,
            'total_fee': self.total_fee,
            'order_time': self.order_time,
            'trans_time': self.trans_time,
            'finnal_amount': self.finnal_amount,
            'discount_amount': self.discount_amount,
            'discount_name': self.discount_name,
            'coupon_info': self.coupon_info,
            'pay_type': self.pay_type,
            'sub_openid': self.sub_openid
        })
        return data 